package com.pj.wisdomlogistics.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.pj.wisdomlogistics.MyApplication;
import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.entity.response.FoodMenu;
import com.pj.wisdomlogistics.mvp.contract.MenuEditorContract;
import com.pj.wisdomlogistics.mvp.frame.MvpActivity;
import com.pj.wisdomlogistics.mvp.frame.base.BaseView;
import com.pj.wisdomlogistics.mvp.presenter.MenuEditorPresenter;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.ui.adapter.MenuEditorAdapter;
import com.pj.wisdomlogistics.ui.view.AutoRadioGroup;
import com.pj.wisdomlogistics.utils.ActivityManagerUtil;
import com.pj.wisdomlogistics.utils.CustomToast;
import com.pj.wisdomlogistics.utils.Log;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by pamper on 2018/2/26.
 */

public class MenuEditorActivity extends MvpActivity<MenuEditorPresenter> implements MenuEditorContract.View, MenuEditorAdapter.onEditor {
    @BindView(R.id.rg_menu)
    AutoRadioGroup rg_menu;
    @BindView(R.id.rb_noodle)
    RadioButton rb_noodle;
    @BindView(R.id.rb_cook)
    RadioButton rb_cook;
    @BindView(R.id.bt_menu_only)
    Button bt_menu;
    @BindView(R.id.rv_menu)
    RecyclerView rv_menu;

    private int type = 0; //0 面食 1 熟食
    private MenuEditorAdapter mAdapter;

    private List<List<FoodMenu>> mList = new ArrayList<>();
    private List<FoodMenu> mMenuList = new ArrayList<>();//原始数据，用来编辑

    @Override
    public void onError(String errorMsg) {
        dismissProgressDialog();
        CustomToast.showToast(this , errorMsg , 1000);
    }

    @Override
    public void onFindSuccess(List<FoodMenu> menuList) {
        mMenuList = menuList;
        dismissProgressDialog();
        if(type == 0){
            mList = averageAssign(menuList , 6);
        }else if(type == 1){
            mList = averageAssign(menuList , 3);
        }
        Log.e("Tag" , "mList----:" + mList);
        mAdapter.setList(mList , type);
    }

    @Override
    public void onSendSuccess() {
        dismissProgressDialog();
        CustomToast.showToast(this , "编辑成功" , 1000);
    }

    @Override
    protected BaseView getBaseView() {
        return this;
    }

    @Override
    public void initView() {
        rv_menu.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MenuEditorAdapter(this);
        rv_menu.setAdapter(mAdapter);

        mAdapter.setListener(this);

        mPresenter.findNoodleFood(MyApplication.userID);
        showProgressDialog();

        for (int i = 0; i < MyApplication.user.getRoleId().size(); i++) {
            if(MyApplication.user.getRoleId().get(i).equals("402848d05f562a0d015f5b46973d0004")){
                //政府大楼餐科长
//                rg_menu.setVisibility(View.VISIBLE);
//                bt_menu.setVisibility(View.GONE);
                rg_menu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        if(checkedId == R.id.rb_noodle){
                            type = 0;
                            mPresenter.findNoodleFood(MyApplication.userID);
                            showProgressDialog();
                            rb_noodle.setTextColor(getResources().getColor(R.color.white));
                            rb_cook.setTextColor(getResources().getColor(R.color.text_normal));
                        }else if(checkedId == R.id.rb_cook){
                            type = 1;
                            mPresenter.findCookFood2(MyApplication.userID);
                            showProgressDialog();
                            rb_noodle.setTextColor(getResources().getColor(R.color.text_normal));
                            rb_cook.setTextColor(getResources().getColor(R.color.white));
                        }
                    }
                });
            }
            else if(MyApplication.user.getRoleId().get(i).equals("402848d05f8edf68015f900ff8da0000")){
                //双和园餐科长
//                rg_menu.setVisibility(View.GONE);
//                bt_menu.setVisibility(View.VISIBLE);

//                type = 0;
//                mPresenter.findNoodleFood(MyApplication.userID);
//                showProgressDialog();
                rg_menu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        if(checkedId == R.id.rb_noodle){
                            type = 0;
                            mPresenter.findNoodleFood(MyApplication.userID);
                            showProgressDialog();
                            rb_noodle.setTextColor(getResources().getColor(R.color.white));
                            rb_cook.setTextColor(getResources().getColor(R.color.text_normal));
                        }else if(checkedId == R.id.rb_cook){
                            type = 1;
                            mPresenter.findCookFood2(MyApplication.userID);
                            showProgressDialog();
                            rb_noodle.setTextColor(getResources().getColor(R.color.text_normal));
                            rb_cook.setTextColor(getResources().getColor(R.color.white));
                        }
                    }
                });
            }
        }

    }

    @Override
    public void initData() {

    }

    @Override
    public int getContentView() {
        return R.layout.activity_menu_editor;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {

    }

    /**
     * 将一个list均分成n个一组list(此方法为已知余数必为0的情况)
     * @param source
     * @return
     */
    public static <T> List<List<T>> averageAssign(List<T> source,int n){
        List<List<T>> result=new ArrayList<>();
        int number= source.size()/n;
        for(int i=0;i<number;i++){
            List<T> value=source.subList(i*n , (i+1)*n);
            result.add(value);
        }
        return result;
    }

    @OnClick({R.id.iv_back , R.id.tv_send})
    public void onClick(View mView){
        switch (mView.getId()){
            case R.id.iv_back:
                ActivityManagerUtil.create().finishActivity();
                break;
            case R.id.tv_send:
                String foodJson = "";
                if(type == 0){
                    mList = averageAssign(mMenuList , 6);
                }else if(type == 1){
                    mList = averageAssign(mMenuList , 3);
                }
                for (int i = 0; i < mList.size(); i++) {
                    for (int j = 0; j < mList.get(i).size(); j++) {
                        foodJson += mList.get(i).get(j).getId() + ","
                                + mList.get(i).get(j).getName() + ","
                                + mList.get(i).get(j).getPrice() + ","
                                + mList.get(i).get(j).getCount() + ";";
                    }
                    foodJson = foodJson.substring(0 , foodJson.length() - 1) + "-";
                }
                foodJson = foodJson.substring(0 , foodJson.length() - 1);
                if(type == 0){
                    mPresenter.modifyNoodleMenu(foodJson , MyApplication.userID);
                }else if(type == 1){
                    mPresenter.modifyMenu(foodJson);
                }
                showProgressDialog();
                break;
        }
    }

    /**
     * tag:一组的第多少个EditText
     * position:第多少组
     * msg:修改后
     * */
    @Override
    public void editor(int tag, int position , String msg) {
        int index = type == 0 ? 6 * position + tag / 3 : 3 * position + tag / 3;
        switch (tag % 3){
            case 0:
                mMenuList.get(index).setName(TextUtils.isEmpty(msg) ? "" : msg);
                break;
            case 1:
                mMenuList.get(index).setPrice(TextUtils.isEmpty(msg) ? 0.0 : Double.parseDouble(msg));
                break;
            case 2:
                mMenuList.get(index).setCount(TextUtils.isEmpty(msg) ? 0 : Integer.parseInt(msg));
                break;
        }
    }
}
